package cn.yx.framework.security;

import cn.yx.framework.configuration.BaseConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESKit {
    private static final Logger logger = LoggerFactory.getLogger(AESKit.class);
    private String mSecretKey = "12345678901234567890123456789012";
    private String mVector = "1234567890123456";
    private static AESKit sAesKit = new AESKit();

    public static AESKit getInstance() {
        return sAesKit;
    }

    public String encrypt(String source) {
        try {
            Cipher ex = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] raw = this.mSecretKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv = new IvParameterSpec(this.mVector.getBytes());
            ex.init(1, skeySpec, iv);
            byte[] encrypted = ex.doFinal(source.getBytes("utf-8"));
            return (new BASE64Encoder()).encode(encrypted);
        } catch (Exception var7) {
            logger.error("encrypt error", var7);
            throw new RuntimeException(var7);
        }
    }

    public static String Encrypt(String encData, String secretKey, String vector) throws Exception {
        if(secretKey == null) {
            logger.error("secret key is null");
            throw new RuntimeException("secret key is null");
        } else if(secretKey.length() != 16) {
            logger.error("secret key length must be 16");
            throw new RuntimeException("secret key length must be 16");
        } else {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] raw = secretKey.getBytes();
            IvParameterSpec iv = new IvParameterSpec(vector.getBytes());
            cipher.init(1, new SecretKeySpec(raw, "AES"), iv);
            byte[] encrypted = cipher.doFinal(encData.getBytes("UTF-8"));
            return (new BASE64Encoder()).encode(encrypted);
        }
    }

    public String decrypt(String source) {
        return this.decrypt(source, this.mSecretKey, this.mVector);
    }

    public String decrypt(String source, String key, String ivs) {
        try {
            byte[] ex = key.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(ex, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
            cipher.init(2, skeySpec, iv);
            byte[] encrypted1 = (new BASE64Decoder()).decodeBuffer(source);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, "utf-8");
        } catch (Exception var10) {
            logger.error("数据解密异常,原始数据:{}:", source, var10);
            throw new RuntimeException(var10);
        }
    }

    public static String encodeBytes(byte[] bytes) {
        StringBuilder strBuf = new StringBuilder();
        byte[] var2 = bytes;
        int var3 = bytes.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            byte aByte = var2[var4];
            strBuf.append((char)((aByte >> 4 & 15) + 97));
            strBuf.append((char)((aByte & 15) + 97));
        }

        return strBuf.toString();
    }

    private AESKit() {
        this.mSecretKey = BaseConstant.getAesConfig().getSecretKey();
        this.mVector = BaseConstant.getAesConfig().getVector();
    }
}
